package com.lcxw.hxzb.friendcircle.project.system.controller;

import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.*;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdMemberMapper;
import com.lcxw.hxzb.friendcircle.project.system.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 游戏信息Controller
 *
 * @author ruoyi
 * @date 2020-09-15
 */
@RestController
@Api(tags = "游戏信息Controller")
@RequestMapping("/client/info")
public class YwpdGameInfoController extends BaseController {
    @Autowired
    private IYwpdGameInfoService ywpdGameInfoService;
    @Autowired
    private IYwpdGamelevelService ywpdGamelevelService;
    @Autowired
    private IYwpdGameService ywpdGameService;
    @Autowired
    private IYwpdGameDistrictService ywpdGameDistrictService;
    @Autowired
    private IYwpdDistrictService ywpdDistrictService;
    @Autowired
    private IYwpdInfoDistrictService ywpdInfoDistrictService;
    @Autowired
    private IYwpdGameOrderService ywpdGameOrderService;
    @Autowired
    private GiftService giftService;
    @Resource
    private YwpdRoomService ywpdRoomService;
    @Autowired
    private YwpdMemberMapper ywpdMemberMapper;

    /**
     * 查询陪玩游戏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(YwpdGameInfo ywpdGameInfo) {
        startPage();
//        ywpdGameInfo.setStatus(1L);
        List<YwpdGameInfo> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        try {
            Map<Long, List<YwpdGamelevel>> levelList = ywpdGamelevelService.selectYwpdGamelevelList(null).stream().collect(Collectors.groupingBy(YwpdGamelevel::getLevelId));
            Map<Long, List<YwpdGame>> gameList = ywpdGameService.selectYwpdGameList(null).stream().collect(Collectors.groupingBy(YwpdGame::getGameId));
            //Map<Long, List<YwpdGameDistrict>> gameDistrictList = ywpdGameDistrictService.selectYwpdGameDistrictList(null).stream().collect(Collectors.groupingBy(YwpdGameDistrict::getDistrictId));
            //查找所有礼物
            Map<Integer, List<Gift>> gifts = giftService.selectAllEnableGift().stream().collect(Collectors.groupingBy(Gift::getId));
            Map<Long, List<YwpdDistrict>> districtList = ywpdDistrictService.selectYwpdDistrictList(null).stream().collect(Collectors.groupingBy(YwpdDistrict::getDistrictId));
            Map<Long, List<YwpdInfoDistrict>> infoDistrictList = ywpdInfoDistrictService.selectYwpdInfoDistrictList(null).stream().collect(Collectors.groupingBy(YwpdInfoDistrict::getInfoId));
            Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(null).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
            //遍历 分别把数据塞到gameInfo
            for (YwpdGameInfo gameInfo : gameInfoList) {
                //发布者信息
                YwpdMember ywpdMember=ywpdMemberMapper.selectYwpdMemberByUserId(gameInfo.getUserId());
                gameInfo.setYwpdMember(ywpdMember);
                //礼物
                Integer giftId = Math.toIntExact(gameInfo.getGiftId());
                List<Gift> giftList = gifts.get(giftId);
                gameInfo.setGift(giftList.get(0));
                //等级
                Long levelId = gameInfo.getLevelId();
                YwpdGamelevel gamelevel = levelList.get(levelId).get(0);
                gameInfo.setYwpdGamelevel(gamelevel);
                //陪玩游戏
                Long gameId = gameInfo.getGameId();
                if(null!=gameList.get(gameId)){
                    YwpdGame game = gameList.get(gameId).get(0);
                    gameInfo.setYwpdGame(game);
                }

                //游戏区服
                Long infoId = gameInfo.getInfoId();
                List<YwpdInfoDistrict> ywpdInfoDistricts = infoDistrictList.get(infoId);
                List<YwpdDistrict> districts = new LinkedList<>();
                for (YwpdInfoDistrict districtL : ywpdInfoDistricts) {
                    Long districtId = districtL.getDistrictId();
                    YwpdDistrict district = districtList.get(districtId).get(0);
                    districts.add(district);
                }
                gameInfo.setYwpdDistrict(districts);
                //游戏评价
                List<YwpdGameOrder> ywpdGameOrders = gameOrderList.get(infoId);
                if (ywpdGameOrders != null) {
                    BigDecimal score = BigDecimal.valueOf(0);
                    for (YwpdGameOrder order : ywpdGameOrders) {
                        if(null!=order.getValue()){
                            BigDecimal value = order.getValue();
                            score = score.add(value);
                        }
                    }
                    BigDecimal scoreDivide = score.divide(BigDecimal.valueOf(ywpdGameOrders.size()),2,BigDecimal.ROUND_HALF_UP);
                    gameInfo.setScore(scoreDivide);
                    gameInfo.setCiShu(Long.valueOf(ywpdGameOrders.size()));
                }else{
                    gameInfo.setScore(new BigDecimal(5));
                    gameInfo.setCiShu(0l);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getDataTable(gameInfoList);
    }



    /**
     * 查询陪玩游戏列表
     */
    @GetMapping("/lists")
    public AjaxResult lists(YwpdGameInfo ywpdGameInfo) {
        Map map=new HashMap();

        ywpdGameInfo.setStatus(1L);
        List<YwpdGameInfo> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        try {
            Map<Long, List<YwpdGamelevel>> levelList = ywpdGamelevelService.selectYwpdGamelevelList(null).stream().collect(Collectors.groupingBy(YwpdGamelevel::getLevelId));
            Map<Long, List<YwpdGame>> gameList = ywpdGameService.selectYwpdGameList(null).stream().collect(Collectors.groupingBy(YwpdGame::getGameId));
            //Map<Long, List<YwpdGameDistrict>> gameDistrictList = ywpdGameDistrictService.selectYwpdGameDistrictList(null).stream().collect(Collectors.groupingBy(YwpdGameDistrict::getDistrictId));
            //查找所有礼物
            Map<Integer, List<Gift>> gifts = giftService.selectAllEnableGift().stream().collect(Collectors.groupingBy(Gift::getId));
            Map<Long, List<YwpdDistrict>> districtList = ywpdDistrictService.selectYwpdDistrictList(null).stream().collect(Collectors.groupingBy(YwpdDistrict::getDistrictId));
            Map<Long, List<YwpdInfoDistrict>> infoDistrictList = ywpdInfoDistrictService.selectYwpdInfoDistrictList(null).stream().collect(Collectors.groupingBy(YwpdInfoDistrict::getInfoId));
            Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(null).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
            //遍历 分别把数据塞到gameInfo
            for (YwpdGameInfo gameInfo : gameInfoList) {
                //发布者信息
                YwpdMember ywpdMember=ywpdMemberMapper.selectYwpdMemberByUserId(gameInfo.getUserId());
                gameInfo.setYwpdMember(ywpdMember);
                //礼物
                Integer giftId = Math.toIntExact(gameInfo.getGiftId());
                List<Gift> giftList = gifts.get(giftId);
                gameInfo.setGift(giftList.get(0));
                //等级
                Long levelId = gameInfo.getLevelId();
                YwpdGamelevel gamelevel = levelList.get(levelId).get(0);
                gameInfo.setYwpdGamelevel(gamelevel);
                //陪玩游戏
                Long gameId = gameInfo.getGameId();
                if(null!=gameList.get(gameId)){
                    YwpdGame game = gameList.get(gameId).get(0);
                    gameInfo.setYwpdGame(game);
                }
                //游戏区服
                Long infoId = gameInfo.getInfoId();
                List<YwpdInfoDistrict> ywpdInfoDistricts = infoDistrictList.get(infoId);
                List<YwpdDistrict> districts = new LinkedList<>();
                for (YwpdInfoDistrict districtL : ywpdInfoDistricts) {
                    Long districtId = districtL.getDistrictId();
                    YwpdDistrict district = districtList.get(districtId).get(0);
                    districts.add(district);
                }
                gameInfo.setYwpdDistrict(districts);
                //游戏评价
                List<YwpdGameOrder> ywpdGameOrders = gameOrderList.get(infoId);
                if (ywpdGameOrders != null) {
                    BigDecimal score = BigDecimal.valueOf(0);
                    for (YwpdGameOrder order : ywpdGameOrders) {
                        if(null!=order.getValue()){
                            BigDecimal value = order.getValue();
                            score = score.add(value);
                        }
                    }
                    BigDecimal scoreDivide = score.divide(BigDecimal.valueOf(ywpdGameOrders.size()),2,BigDecimal.ROUND_HALF_UP);
                    gameInfo.setScore(scoreDivide);
                    gameInfo.setCiShu(Long.valueOf(ywpdGameOrders.size()));
                }else{
                    gameInfo.setScore(new BigDecimal(5));
                    gameInfo.setCiShu(0l);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //判断当前陪玩者是否有正在进行中的游戏
        List<YwpdGameOrder> sizes=ywpdGameOrderService.selectOrderLists(ywpdGameInfo.getUserId());
        if(sizes.size()>0){
            map.put("status",1);
        }else{
            map.put("status",0);
        }
        map.put("data",gameInfoList);
        return AjaxResult.success(map);
    }


    /*
     *  查询开黑列表 通过游戏类型查询房间
     * */

    @GetMapping("/blackList")
    public AjaxResult blackList(YwpdGameInfo ywpdGameInfo) {
        startPage();
        ywpdGameInfo.setStatus(1L);
        List<YwpdGameInfo> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        List<YwpdRoom> ywpdRooms = ywpdRoomService.selectAllLivingRoom();
        List<YwpdRoom> rooms = new LinkedList<>();
        if (ywpdRooms.size() != 0) {
            //查所有在播房间
            Map<Long, List<YwpdRoom>> ywpdRoomList = ywpdRooms.stream().collect(Collectors.groupingBy(YwpdRoom::getInfoId));
            for (YwpdGameInfo gameInfo : gameInfoList) {
                Long infoId = gameInfo.getInfoId();
                YwpdRoom ywpdRoom = ywpdRoomList.get(infoId).get(0);
                rooms.add(ywpdRoom);
            }
        }
        return AjaxResult.success(rooms);
    }

    /**
     * 获取游戏信息详细信息
     */
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return AjaxResult.success(ywpdGameInfoService.selectYwpdGameInfoById(infoId));
    }

    /**
     * 新增游戏信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody YwpdGameInfo ywpdGameInfo) {
        ywpdGameInfo.setStatus(0L);
        ywpdGameInfoService.insertYwpdGameInfo(ywpdGameInfo);
        YwpdInfoDistrict infoDistrict = new YwpdInfoDistrict();
        infoDistrict.setInfoId(ywpdGameInfo.getInfoId());
        List<Long> districtId = ywpdGameInfo.getDistrictId();
        for (Long aLong : districtId) {
            infoDistrict.setDistrictId(aLong);
            ywpdInfoDistrictService.insertYwpdInfoDistrict(infoDistrict);
        }
        return AjaxResult.success();
    }

    /**
     * 修改游戏信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdGameInfo ywpdGameInfo) {

        return toAjax(ywpdGameInfoService.updateYwpdGameInfo(ywpdGameInfo));
    }

    /**
     * 删除游戏信息
     */
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(ywpdGameInfoService.deleteYwpdGameInfoByIds(infoIds));
    }


    /**
     * 查询所有礼物
     */
    @GetMapping("/getGift")
    public AjaxResult getGift() throws Exception {
        List<Gift> gifts = giftService.selectAllEnableGift();
        return AjaxResult.success(gifts);
    }
}
